package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.app.Dialog;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.imgedit.view.imagezoom.ImageViewTouchBase;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.StickerAdapter;
import flc.ast.bean.MyStickerBean;
import flc.ast.databinding.ActivityPicStickerBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;

public class PicStickerActivity extends BaseAc<ActivityPicStickerBinding> {

    public static String imgPath = "";
    private StickerAdapter stickerAdapter;
    private int oldPosition;
    private Dialog myTipDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_pic_sticker;
    }

    @Override
    public void onBackPressed() {
        myTipDialog.show();
    }

    @Override
    protected void initView() {
        mDataBinding.ivPicStickerBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivStickerSave.setOnClickListener(this);
        mDataBinding.rvStickerList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL,false));
        stickerAdapter = new StickerAdapter();
        mDataBinding.rvStickerList.setAdapter(stickerAdapter);
        stickerAdapter.setOnItemClickListener(this);
        //提示弹框
        tipDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivStickerSave:
                if (mDataBinding.ivStickerView.hasSticker()) {
                    mDataBinding.ivStickerView.setShowHelpToolFlag(false);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            saveImg();
                        }
                    }, 500);
                }
                break;
            //提示弹框
            case R.id.ivDialogTipCancel://取消
                myTipDialog.dismiss();
                break;
            case R.id.ivDialogTipRight://确定
                myTipDialog.dismiss();
                finish();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        stickerAdapter.getItem(oldPosition).setSelect(false);
        stickerAdapter.notifyItemChanged(oldPosition);
        oldPosition=position;
        stickerAdapter.getItem(position).setSelect(true);
        stickerAdapter.notifyItemChanged(position);
        Resources res = getResources();
        Bitmap bmp = BitmapFactory.decodeResource(res, stickerAdapter.getItem(position).getStickerImg());
        mDataBinding.ivStickerView.addBitImage(bmp);
    }

    @Override
    protected void initData() {
        //获取图片
        if (imgPath == null || TextUtils.isEmpty(imgPath)) return;
        Glide.with(this).load(imgPath).into(mDataBinding.imageViewTouch);
        mDataBinding.imageViewTouch.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        mDataBinding.imageViewTouch.setScaleEnabled(false);// 禁用缩放
        //贴纸数据
        List<MyStickerBean> listSticker = new ArrayList<>();
        listSticker.add(new MyStickerBean(R.drawable.bq1, R.drawable.bqq1, false));
        listSticker.add(new MyStickerBean(R.drawable.bq2, R.drawable.bqq2, false));
        listSticker.add(new MyStickerBean(R.drawable.bq3, R.drawable.bqq3, false));
        listSticker.add(new MyStickerBean(R.drawable.bq4, R.drawable.bqq4, false));
        listSticker.add(new MyStickerBean(R.drawable.bq5, R.drawable.bqq5, false));
        listSticker.add(new MyStickerBean(R.drawable.bq6, R.drawable.bqq11, false));
        listSticker.add(new MyStickerBean(R.drawable.bq7, R.drawable.bqq12, false));
        listSticker.add(new MyStickerBean(R.drawable.bq8, R.drawable.bqq13, false));
        listSticker.add(new MyStickerBean(R.drawable.bq9, R.drawable.bqq14, false));
        listSticker.add(new MyStickerBean(R.drawable.bq10, R.drawable.bqq15, false));
        listSticker.add(new MyStickerBean(R.drawable.bq11, R.drawable.bqq6, false));
        listSticker.add(new MyStickerBean(R.drawable.bq12, R.drawable.bqq7, false));
        listSticker.add(new MyStickerBean(R.drawable.bq13, R.drawable.bqq8, false));
        listSticker.add(new MyStickerBean(R.drawable.bq14, R.drawable.bqq9, false));
        listSticker.add(new MyStickerBean(R.drawable.bq15, R.drawable.bqq10, false));
        stickerAdapter.setList(listSticker);
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.save_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.rlStickerSave);
                        ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_suc);
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }


    //提示弹框
    private void tipDialog() {
        myTipDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_tip, null);
        myTipDialog.setContentView(inflate);
        myTipDialog.setCancelable(true);
        Window dialogWindow = myTipDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView cancel = inflate.findViewById(R.id.ivDialogTipCancel);
        ImageView right = inflate.findViewById(R.id.ivDialogTipRight);
        TextView textView=inflate.findViewById(R.id.tvDialogTipText);
        textView.setText(getString(R.string.image_not_save));
        cancel.setOnClickListener(this);
        right.setOnClickListener(this);
    }

}